package per.sunmes.lesrb.util;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

/** DrawU */
public class DU {

	private static final Color tmpDrawColor = new Color();

	public static void drawTexture(Batch batch, float parentAlpha, TextureRegion tr, Color color, float x, float y,
			float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation) {
		Color oldColor = batch.getColor();
		tmpDrawColor.set(color.r, color.g, color.b, color.a * parentAlpha);
		batch.setColor(tmpDrawColor);
		batch.draw(tr, x, y, originX, originY, width, height, scaleX, scaleY, rotation);
		batch.setColor(oldColor);
	}

	public static void drawTextureByActor(Batch batch, float parentAlpha, TextureRegion tr, Actor target) {
		Color oldColor = batch.getColor();
		tmpDrawColor.set(target.getColor().r, target.getColor().g, target.getColor().b,
				target.getColor().a * parentAlpha);
		batch.setColor(tmpDrawColor);
		batch.draw(tr, target.getX(), target.getY(), target.getOriginX(), target.getOriginY(), target.getWidth(),
				target.getHeight(), target.getScaleX(), target.getScaleY(), target.getRotation());
		batch.setColor(oldColor);
	}

	public static void drawTextureByActor(Batch batch, float parentAlpha, TextureRegion tr, Actor target,
			float[] overflow) {
		Color oldColor = batch.getColor();
		tmpDrawColor.set(target.getColor().r, target.getColor().g, target.getColor().b,
				target.getColor().a * parentAlpha);
		batch.setColor(tmpDrawColor);
		batch.draw(tr, target.getX() - overflow[0], target.getY() - overflow[2], target.getOriginX(),
				target.getOriginY(), target.getWidth() + overflow[0] + overflow[1],
				target.getHeight() + overflow[2] + overflow[3], target.getScaleX(), target.getScaleY(),
				target.getRotation());
		batch.setColor(oldColor);
	}

	public static void drawTextureByActorCenter(Batch batch, float parentAlpha, TextureRegion tr, Actor target) {
		Color oldColor = batch.getColor();
		tmpDrawColor.set(target.getColor().r, target.getColor().g, target.getColor().b,
				target.getColor().a * parentAlpha);
		batch.setColor(tmpDrawColor);
		float ox = target.getWidth() / 2 - tr.getRegionWidth() / 2;
		float oy = target.getHeight() / 2 - tr.getRegionHeight() / 2;
		batch.draw(tr, target.getX() + ox, target.getY() + oy, target.getOriginX(), target.getOriginY(),
				tr.getRegionWidth(), tr.getRegionHeight(), target.getScaleX(), target.getScaleY(),
				target.getRotation());
		batch.setColor(oldColor);
	}

	public static void drawTextureCustomRect(Batch batch, float parentAlpha, TextureRegion tr, Actor target, float tX,
			float tY, float tWidth, float tHeight, int sX, int sY, int sWidth, int sHeight) {
		Color oldColor = batch.getColor();
		tmpDrawColor.set(target.getColor().r, target.getColor().g, target.getColor().b,
				target.getColor().a * parentAlpha);
		batch.setColor(tmpDrawColor);
		// batch.draw(tr.getTexture(), tX, tY, tWidth, tHeight, sX, sY, sWidth,
		// sHeight, false, false);
		batch.draw(tr.getTexture(), tX, tY, target.getOriginX(), target.getOriginY(), tWidth, tHeight,
				target.getScaleX(), target.getScaleY(), target.getRotation(), sX, sY, sWidth, sHeight, false, false);
		batch.setColor(oldColor);
	}

	public static void drawTextureBaseDownHeightPercent(Batch batch, float parentAlpha, TextureRegion tr, Actor target,
			float x, float y, float width, float height, float percent) {
		drawTextureCustomRect(batch, parentAlpha, tr, target, x, y, width, height * percent, tr.getRegionX(),
				tr.getRegionY() + (int) (tr.getRegionHeight() * (1 - percent)), tr.getRegionWidth(),
				(int) (tr.getRegionHeight() * percent));
	}

	public static void drawTextureBaseUpHeightPercent(Batch batch, float parentAlpha, TextureRegion tr, Actor target,
			float x, float y, float width, float height, float percent) {
		drawTextureCustomRect(batch, parentAlpha, tr, target, x, y + (height * (1 - percent)), width, height * percent,
				tr.getRegionX(), tr.getRegionY(), tr.getRegionWidth(), (int) (tr.getRegionHeight() * percent));
	}

	static int oldBlendSrc, oldBlendDst;
	static boolean oldIsBlendOn;

	public static void drawBlendOn(Batch batch) {
		oldIsBlendOn = batch.isBlendingEnabled();
		oldBlendSrc = batch.getBlendSrcFunc();
		oldBlendDst = batch.getBlendDstFunc();
		if (!oldIsBlendOn) {
			batch.enableBlending();
		}
		batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
	}

	public static void drawBlendOff(Batch batch) {
		if (!oldIsBlendOn) {
			batch.disableBlending();
		}
		batch.setBlendFunction(oldBlendSrc, oldBlendDst);
	}

}
